﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;
namespace YT.XH.NOSE
{
    public partial class frmNSTreatmentHistoryDetail : Form
    {
        private string mstrSeqNo = string.Empty;
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormStatus mfrmStatus = new FormStatus();
        private bool mblnExit = false;
        private string mstrTableName = "NoseTreatmentHistoryDtl";

        public frmNSTreatmentHistoryDetail(string strSeqNo,string  strSickRecID)
        {
            InitializeComponent();
            mstrSeqNo = strSeqNo;
            mstrSickRecID = strSickRecID;
            if (strSeqNo == "" || strSeqNo == "0")
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;
        }

        private void FillData()
        {
            string strSQL = string.Empty;
            if (mfrmStatus == FormStatus.AddNew) return;

            strSQL = "select * from "+ mstrTableName  +" where SeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mstrSickRecID +"'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtSeqNo.Text = CvtFunc.ToString(drRow["InjectSeqNo"]);
                txtWeekly.Text = CvtFunc.ToString(drRow["Weekly"]);
                udtScheduleDate.Value = BaseFunctions.FormatDate(drRow["ScheduleDate"].ToString(), DateFormatOption.DateFmtYMD);
                txtLabLotNo.Text = CvtFunc.ToString(drRow["LabLotNo"]);
                txtInjectQuantity.Text = CvtFunc.ToString(drRow["InjectQuantity"]);
                udtInjectDate.Value = BaseFunctions.FormatDate(drRow["InjectDate"].ToString(), DateFormatOption.DateFmtYMD);
                txtPEFRBeforInject.Text = CvtFunc.ToString(drRow["PEFRBeforInject"]);
                txtVAS.Text = CvtFunc.ToString(drRow["VAS"]);
                txtUntowardEffect.Text = CvtFunc.ToString(drRow["UntowardEffect"]);
                txtRemark.Text = CvtFunc.ToString(drRow["Remark"]);
                txtOraldrugName.Text = CvtFunc.ToString(drRow["OralMedicine"]);
                txtOraldrugTimes.Text = CvtFunc.ToString(drRow["OralMedicineTimes"]);
                txtSparydrugName.Text = CvtFunc.ToString(drRow["Spray"]);
                txtSparydrugTimes.Text = CvtFunc.ToString(drRow["SprayTimes"]);
                txtOthers.Text = CvtFunc.ToString(drRow["Others"]);
                string strTempValue = string.Empty;
                #region 鼻塞
                strTempValue = CvtFunc.ToString(drRow["Rhinobyon"]);
                if (strTempValue == "无") rad1_1.Checked = true;
                if (strTempValue == "轻度") rad1_2.Checked = true;
                if (strTempValue == "中度") rad1_3.Checked = true;
                if (strTempValue == "重度") rad1_4.Checked = true;
                #endregion
                #region 鼻痒
                strTempValue = CvtFunc.ToString(drRow["Rhinocnesmus"]);
                if (strTempValue == "无") rad2_1.Checked = true;
                if (strTempValue == "轻度") rad2_2.Checked = true;
                if (strTempValue == "中度") rad2_3.Checked = true;
                if (strTempValue == "重度") rad2_4.Checked = true;
                #endregion
                #region 流涕
                strTempValue = CvtFunc.ToString(drRow["Turbidnasaldischarge"]);
                if (strTempValue == "无") rad3_1.Checked = true;
                if (strTempValue == "轻度") rad3_2.Checked = true;
                if (strTempValue == "中度") rad3_3.Checked = true;
                if (strTempValue == "重度") rad3_4.Checked = true;
                #endregion
                #region 喷嚏
                strTempValue = CvtFunc.ToString(drRow["Sneeze"]);
                if (strTempValue == "无") rad4_1.Checked = true;
                if (strTempValue == "轻度") rad4_2.Checked = true;
                if (strTempValue == "中度") rad4_3.Checked = true;
                if (strTempValue == "重度") rad4_4.Checked = true;
                #endregion                                                
            }
        }

        private bool UpdateDetail()
        {
            if (!ValidCheck()) return false; 
            string strSQL = string.Empty;
            DataRow drRow = null;
            string strSeqNo = string.Empty;
            string strTempValue = string.Empty;
            if (mfrmStatus == FormStatus.Update)
            {
                strSQL = "select top 1 * from " + mstrTableName + " where SeqNo='" + mstrSeqNo + "' and SickRecID='"+ mstrSickRecID +"' ";
                drRow = mdbcon.GetDataFirstRow(strSQL);
                strSeqNo = CvtFunc.ToString(drRow["SeqNo"]);
            }
            else if (mfrmStatus == FormStatus.AddNew)
            {
                drRow = mdbcon.GetDataTable("select top 0 * from " + mstrTableName + " ").NewRow();
                strSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue(mstrTableName, "max(SeqNo) as SeqNo", " 1=1 and SickRecID='"+ mstrSickRecID +"'")) + 1).ToString();
            }
            #region 鼻塞
            if (rad1_1.Checked) strTempValue = rad1_1.Text;
            if (rad1_2.Checked) strTempValue = rad1_2.Text;
            if (rad1_3.Checked) strTempValue = rad1_3.Text;
            if (rad1_4.Checked) strTempValue = rad1_4.Text;
            drRow["Rhinobyon"] = strTempValue;
            #endregion

            #region 鼻痒
            if (rad2_1.Checked) strTempValue = rad2_1.Text;
            if (rad2_2.Checked) strTempValue = rad2_2.Text;
            if (rad2_3.Checked) strTempValue = rad2_3.Text;
            if (rad2_4.Checked) strTempValue = rad2_4.Text;
            drRow["Rhinocnesmus"] = strTempValue;
            #endregion

            #region 流涕
            if (rad3_1.Checked) strTempValue = rad3_1.Text;
            if (rad3_2.Checked) strTempValue = rad3_2.Text;
            if (rad3_3.Checked) strTempValue = rad3_3.Text;
            if (rad3_4.Checked) strTempValue = rad3_4.Text;
            drRow["Turbidnasaldischarge"] = strTempValue;
            #endregion

            #region 喷嚏
            if (rad4_1.Checked) strTempValue = rad4_1.Text;
            if (rad4_2.Checked) strTempValue = rad4_2.Text;
            if (rad4_3.Checked) strTempValue = rad4_3.Text;
            if (rad4_4.Checked) strTempValue = rad4_4.Text;
            drRow["Sneeze"] = strTempValue;
            #endregion       

            drRow["SeqNo"] = strSeqNo; 
            drRow["InjectSeqNo"] = CvtFunc.ToInt(txtSeqNo.Text); //strSeqNo;
            drRow["Weekly"] = txtWeekly.Text;
            if (udtScheduleDate.Text !="    -  -  ")            
            drRow["ScheduleDate"] =CvtFunc.ToDateTime( udtScheduleDate.Text );
            drRow["LabLotNo"] = txtLabLotNo.Text;
            drRow["InjectQuantity"] = txtInjectQuantity.Text;
            drRow["InjectDate"] = BaseFunctions.FormatDate(udtInjectDate.Value, DateFormatOption.DateFmtYMD);
            drRow["PEFRBeforInject"] = txtPEFRBeforInject.Text;
            drRow["VAS"] = txtVAS.Text;
            drRow["UntowardEffect"] = txtUntowardEffect.Text;
            drRow["Remark"] = txtRemark.Text;
            drRow["OralMedicine"] = txtOraldrugName.Text;
            drRow["OralMedicineTimes"] =CvtFunc.ToDouble( txtOraldrugTimes.Text);
            drRow["Spray"] = txtSparydrugName.Text;
            drRow["SprayTimes"] =CvtFunc.ToDouble( txtSparydrugTimes.Text);
            drRow["Others"] = txtOthers.Text;
            drRow["SickRecID"] = mstrSickRecID;
            string strFields = @"SeqNo,InjectSeqNo,Weekly,ScheduleDate,LabLotNo,InjectQuantity,InjectDate,PEFRBeforInject,
                                VAS,UntowardEffect,Remark,OralMedicine,OralMedicineTimes,Spray,SprayTimes,Others,
                                Rhinobyon,Rhinocnesmus,Sneeze,Turbidnasaldischarge,SickRecID";

            if (mfrmStatus == FormStatus.AddNew)
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.AddNew);
            else
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.Update);
            return true;
        }

        private bool ValidCheck()
        {
            string strErrMSG = string.Empty;
            if (txtSeqNo.Text == "")
            {
                strErrMSG = "请填写序号。";
            }
            else if (!rad1_1.Checked && !rad1_2.Checked && !rad1_3.Checked && !rad1_4.Checked)
            {
                strErrMSG = "请选择 鼻塞 的主要症状。";
            }
            else if (!rad2_1.Checked && !rad2_2.Checked && !rad2_3.Checked && !rad2_4.Checked)
            {
                strErrMSG = "请选择 鼻痒 的主要症状。";
            }
            else if (!rad3_1.Checked && !rad3_2.Checked && !rad3_3.Checked && !rad3_4.Checked)
            {
                strErrMSG = "请选择 流涕 的主要症状。";
            }
            else if (!rad4_1.Checked && !rad4_2.Checked && !rad4_3.Checked && !rad4_4.Checked)
            {
                strErrMSG = "请选择 喷嚏 的主要症状。";
            }
            //else if ( udtScheduleDate.Text=="    -  -  ")
            //{
            //    strErrMSG = "预定注射日期 日期不能为空。";
            //}
            else if (udtInjectDate.Text == "    -  -  ")
            {
                strErrMSG = "注射日期 日期不能为空。";
            }
            if (strErrMSG != "")
            {
                MessageBox.Show(strErrMSG, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
                return true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
            {
                timer1.Stop();
            }
            this.Opacity += 0.04;
        }

        private void frmNSTreatmentHistoryDetail_Load(object sender, EventArgs e)
        {
            this.Text = "脱敏注射治疗记录表";
            FillData();
            timer1.Start();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!UpdateDetail()) return;
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (!UpdateDetail()) return; 
            }
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您是否确定要删除此记录？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string strSQL = "Delete "+ mstrTableName  +"  where SeqNo='" + mstrSeqNo + "' and SickRecID='" + mstrSickRecID + "'";
                mdbcon.ExecuteSql(strSQL);
                this.Close();
            }
        }
    }
}
